package bill.study.javaPuzzlers.classy;

/**
 * The problem of circular instance initialization arises whenever a constructor
 * calls a method that has been overriden in its subclass. A method invoked in
 * this way always runs before the instance has been initialized, when its
 * declared fields still have their default values.
 * 
 * You must never call an overridable method from a constructor under any
 * circumstances. This prohibition extends to instance initializers and the
 * bodies of the pseudoconstructors readObject and clone. The resulting
 * circularities initialization can be fatal. the solution to this problem is
 * lazy initialization.
 * 
 * 
 * @author yangliu
 * 
 */
class Point {
	private final int x, y;
	private final String name;

	Point(int x, int y) {
		this.x = x;
		this.y = y;
		this.name = makeName();
	}

	protected String makeName() {
		return "[" + x + "'" + y + "]";
	}

	public final String toString() {
		return name;
	}
}

public class ColorPoint extends Point {
	private final String color;

	public ColorPoint(int x, int y, String color) {
		super(x, y);
		this.color = color;
	}

	protected String makeName() {
		return super.makeName() + ":" + this.color;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(new ColorPoint(4, 2, "purple"));

	}

}
